using FytSoa.Domain.Car;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Domain.Core.Result;
using FytSoa.Domain.User;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Car;

/// <summary>
/// 服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v3")]
public class CarsPrizelogService : IApplicationService
{
    private readonly SugarRepository<CarsPrizelog> _thisRepository;
    public CarsPrizelogService(SugarRepository<CarsPrizelog> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<CarsPrizelogDto>> GetPagesAsync(PrizePageParam param)
    {
        var where = Expressionable.Create<CarsPrizelog>();
        if (param.Id!=0)
        {
            where.And(m => m.UserId == param.Id);
        }
        if (param.PrizeId!=0)
        {
            where.And(m => m.PrizeId == param.PrizeId);
        }
        var query = await _thisRepository.AsQueryable()
            .Where(where.ToExpression())
            .ToPageAsync(param.Page, param.Limit);
        var result = query.Adapt<PageResult<CarsPrizelogDto>>();
        var userIds = result.Items.Select(m => m.UserId).ToList();
        var userList = new List<Member>();
        if (userIds.Count>0)
        {
            var userRepository = _thisRepository.ChangeRepository<SugarRepository<Member>>();
            userList = await userRepository.GetListAsync(m => userIds.Contains(m.Id));
        }

        var prizeIds = result.Items.Select(m => m.PrizeId).ToList();
        var prizeList = new List<CarsPrize>();
        if (prizeIds.Count>0)
        {
            var prizeRepository = _thisRepository.ChangeRepository<SugarRepository<CarsPrize>>();
            prizeList = await prizeRepository.GetListAsync(m => prizeIds.Contains(m.Id));
        }

        foreach (var item in result.Items)
        {
            item.User = userList.FirstOrDefault(m => m.Id == item.UserId) ?? new Member();
            item.Prize = prizeList.FirstOrDefault(m => m.Id == item.PrizeId) ?? new CarsPrize();
        }
        return result;
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<CarsPrizelogDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<CarsPrizelogDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(CarsPrizelogDto model) =>
        await _thisRepository.InsertAsync(model.Adapt<CarsPrizelog>());

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(CarsPrizelogDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<CarsPrizelog>());
    
    /// <summary>
    /// 处理查看状态
    /// </summary>
    /// <returns></returns>
    public async Task<bool> ModifyStatusAsync(List<long> ids)=>
        await _thisRepository.UpdateAsync(m=>new CarsPrizelog()
    {
        Status = true
    },m=>ids.Contains(m.Id));

    /// <summary>
    /// 删除,支持多个
    /// </summary>
    /// <param name="ids">逗号分隔</param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync(string ids) =>
        await _thisRepository.DeleteAsync(m => ids.StrToListLong().Contains(m.Id));
}
